{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python374jvsc74a57bd02a7e95a32014fc1ccf24626d45a98c6e7b4373277259c22f47a91d487fc3e8a5",
   "display_name": "Python 3.7.4 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "r = requests.get('http://data.eastmoney.com/stock/lhb.html')\n",
    "r.raise_for_status()\n",
    "r.encoding = \"gb2312\"\n",
    "soup = BeautifulSoup(r.text, 'html.parser')    \n",
    "a = soup.find_all('span',class_=\"wname\")\n",
    "for i in a:\n",
    "    print(i.find('a')['data_code'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "infoDict = {}\n",
    "\n",
    "headers = {\n",
    "    'Connection': 'keep-alive',\n",
    "    'Cache-Control': 'max-age=0',\n",
    "    'Upgrade-Insecure-Requests': '1',\n",
    "    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',\n",
    "    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',\n",
    "    'Accept-Language': 'zh-CN,zh;q=0.9',\n",
    "}\n",
    "\n",
    "r = requests.get('http://stockpage.10jqka.com.cn/600007/', headers=headers, verify=False)\n",
    "r.raise_for_status()\n",
    "r.encoding = \"utf-8\"\n",
    "soup = BeautifulSoup(r.text, 'html.parser')  \n",
    "\n",
    "# name = soup.find('strong',id ='stockNamePlace').text\n",
    "name = soup.find('strong',attrs={'id':'stockNamePlace'}).text\n",
    "infoDict.update({'股票名称': name})\n",
    "\n",
    "stockInfo = soup.find('dl',class_=\"company_details\")\n",
    "keyList = stockInfo.find_all('dt')\n",
    "valueList = stockInfo.find_all('dd')\n",
    "del valueList[2]\n",
    "for i in range(len(keyList)):\n",
    "    if valueList[i].attrs == {}:\n",
    "        val = valueList[i].text\n",
    "    else :\n",
    "        val = valueList[i].attrs['title']\n",
    "    key = keyList[i].text\n",
    "    infoDict[key] = val     #主营业务有两个'dd',所以错位了\n",
    "infoDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('BaiduStockInfo.txt', 'a', encoding='utf-8') as f:\n",
    "    f.write(' str(infoDict) '+ '\\n' )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in reversed(range(len([1,2,3,4,5]))):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "{\"股票名称\": \"中国国贸600007\", \"流通股：\": \"10.07亿\", \"总股本：\": \"10.07亿\", \"每股未分配利润：\": \"4.83元\", \"每股公积金：\": \"1.86元\", \"每股现金流：\": \"0.48元\", \"营业收入：\": \"8.39亿元\", \"净利润增长率：\": \"16.53%\", \"净利润：\": \"2.38亿元\", \"每股收益：\": \"0.24元\", \"每股净资产：\": \"8.19元\", \"上市日期：\": \"1999-03-12\", \"主营业务：\": \"\\u3000\\u3000为国内外经贸企业和商务人员及旅游者提供并经营下列项目：写字楼及楼宇管理服务、宾馆、公寓（国贸世纪公寓）及配套的餐饮（含加工制售食品、销售定型包装食品）、宴会、会议、展览、展销、商场（含出租以及图书、期刊、报纸的零售）、幼儿园（主要为商务人员子女）、商务中心、汽车综合服务、收费停车场、文体娱乐（含桑那浴、按摩室、水疗、球类）及相关服务；提供对外经贸信息和咨询服务；利用本中心广告牌发布广告。\", \"涉及概念：\": \"标普道琼斯A股，中字头股票，融资融券，沪股通\", \"所属地域：\": \"北京市\"}\n\n['股票名称', '流通股：', '总股本：', '每股未分配利润：', '每股公积金：', '每股现金流：', '营业收入：', '净利润增长率：', '净利润：', '每股收益：', '每股净资产：', '上市日期：', '主营业务：', '涉及概念：', '所属地域：']\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import os\n",
    "from openpyxl import Workbook\n",
    "\n",
    "cols = []\n",
    "jsfile = r\"F:/NOW/job/KtoE/TongHuaShunStockInfo.json\"\n",
    "if os.path.exists(jsfile):\n",
    "    # 先用key值写表头\n",
    "    with open(jsfile, 'r',encoding='utf8') as fp:\n",
    "        # 先用key值写表头\n",
    "        line = fp.readline()\n",
    "        print (line)\n",
    "        if not line:\n",
    "            print(\"没有内容\")\n",
    "        else:\n",
    "            # 每一行转换成字典类型\n",
    "            jsdata = json.loads(line, strict=False)\n",
    "            # 用key值做标题\n",
    "            for k in jsdata.keys():\n",
    "                if k not in cols:\n",
    "                    cols.append(k)\n",
    "print(cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}